package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.model.DigestInfo;
import la.iok.hzsvn.lewin.movie.model.MovieSceneParam;
import la.iok.hzsvn.lewin.movie.model.MovieSceneVo;
import la.iok.hzsvn.lewin.movie.model.ObsSignedUrl;
import la.iok.hzsvn.lewin.movie.service.MovieSceneService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("movieScenes")
public class MovieSceneController {
    private final MovieSceneService movieSceneService;

    public MovieSceneController(MovieSceneService movieSceneService) {
        this.movieSceneService = movieSceneService;
    }

    /**
     * 保存剧幕信息
     * @param token 操作用户
     * @param id 剧本id
     * @param param 剧幕信息
     * @return 操作结果，包含保存剧幕的id
     */
    @PostMapping("movie/{id}")
    public SingleDataResponse<Long> save(@RequestHeader Token token, @PathVariable Long id, @RequestBody MovieSceneParam param){
        return new SingleDataResponse<>(movieSceneService.save(token,id,param));
    }

    /**
     * 上传剧幕文件
     * @param token 操作用户
     * @param id 剧幕id
     * @param file 封面文件
     * @return 操作结果
     */
    @PostMapping("{id}/file")
    public OperatorResponse uploadFile(@RequestHeader Token token, @PathVariable Long id, MultipartFile file){
        movieSceneService.uploadFile(token,id,file);
        return OperatorResponses.success();
    }

    /**
     * 修改剧幕信息
     * @param token 操作用户
     * @param id 剧幕id
     * @param param 剧幕信息
     * @return 操作结果
     */
    @PutMapping("{id}")
    public OperatorResponse update(@RequestHeader Token token, @PathVariable Long id, @RequestBody MovieSceneParam param) {
        movieSceneService.update(token,id,param);
        return OperatorResponses.success();
    }

    /**
     * 删除剧幕
     * @param token 操作用户
     * @param id 剧幕id
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token, @PathVariable Long id){
        movieSceneService.delete(token,id);
        return OperatorResponses.success();
    }

    @GetMapping("{id}/digest")
    public SingleDataResponse<String> digest(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(movieSceneService.digest(id));
    }

    @PutMapping("{id}/digest")
    public SingleDataResponse<ObsSignedUrl> fileDigest(@RequestHeader Token token, @PathVariable Long id, @RequestBody DigestInfo digest){
        return new SingleDataResponse<>(movieSceneService.uploadFile(token,id,digest));
    }

    @GetMapping("{id}/storePath")
    public SingleDataResponse<String> storePath(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(movieSceneService.storePath(id));
    }

    @GetMapping("{id}/obs")
    public SingleDataResponse<ObsSignedUrl> obsGet(@RequestHeader Token token,@PathVariable Long id){
        return new SingleDataResponse<>(movieSceneService.obsGet(token,id));
    }

    /**
     * 查询剧本的所有剧幕
     * @param token 操作用户
     * @param id 剧本id
     * @return 剧幕列表
     */
    @GetMapping("movie/{id}")
    public ListDataResponse<MovieSceneVo> scenes(@RequestHeader Token token, @PathVariable Long id){
        return new ListDataResponse<>(movieSceneService.movieScenes(token,id));
    }

    @GetMapping("{id}/file")
    public void downloadFile(@RequestHeader Token token, HttpServletResponse response, @PathVariable Long id){
        movieSceneService.downloadFile(token,response,id);
    }
}
